from fastapi import FastAPI
import asyncio

from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from hooks.lifespan import lifespan
from hooks.middlewares import log_middleware
from routers import user ,address

# from custom_docs import get_swagger_ui_html

import uvicorn
import settings

app=FastAPI(lifespan=lifespan)

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源，生产环境建议指定具体域名
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有HTTP方法
    allow_headers=["*"],  # 允许所有请求头
)
# app.mount("/static", StaticFiles(directory="static"), name="static")

# @app.get("/static/swagger-ui/swagger-ui.css.map", include_in_schema=False)
# async def swagger_css_map():
#     return Response(status_code=204)

# @app.get("/docs", include_in_schema=False)
# async def custom_swagger_ui_html():
#     return get_swagger_ui_html(
#         openapi_url=app.openapi_url,
#         title=f"{app.title} - Swagger UI",
#     )
app.add_middleware(BaseHTTPMiddleware, dispatch=log_middleware)
app.include_router(user.router)
app.include_router(address.router)

@app.get("/health")
async def health_check():
    return {"status": "healthy"}

if __name__ == "__main__":
    uvicorn.run(
        app="main:app",  # 也可直接传 app 实例：app=app
        host="0.0.0.0",
        port=settings.SERVER_PORT,
        reload=True  # 开发环境热重载
    )
